package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//201521123059
public class EchoServer {
	public static void menu(Scanner in, PrintWriter out, Socket incoming) throws Exception {
		out.println("1-Time , 2-Ip  , 3-File , 4-quit menu");
		while (in.hasNextLine()) {
			String line = in.nextLine();
			if (line.trim().equals("1")) {
				Date data = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");
				String time = dateFormat.format(data);
				out.println(time);
			}
			if (line.trim().equals("2")) {
				out.println(" IP and port: " + incoming.getInetAddress().toString() + ":" + incoming.getPort());
			}
			if (line.trim().equals("3")) {
				out.println("Enter File name:");
				out.println();
			}
			if (line.trim().equals("4")) {
				out.println("quit success");
				break;
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 try
	      {  
		     //establish server socket
	         int i = 1;
	         // wait for client connection
	         ServerSocket s = new ServerSocket(8190);
	         while (true)
	         {  
	            Socket incoming = s.accept();
	            System.out.println("Spawning " + i);
	            Runnable r = new ThreadedEchoHandler(incoming);
	            Thread t = new Thread(r);
	            t.start();
	            i++;
	         }
	      }
	      catch (IOException e)
	      {  
	         e.printStackTrace();
	      }

	}

}

class ThreadedEchoHandler implements Runnable
{ 
   /**
      Constructs a handler.
      @param i the incoming socket
      @param c the counter for the handlers (used in prompts)
   */
   public ThreadedEchoHandler(Socket i)
   { 
      incoming = i; 
   }

   public void run()
   {  
      try
      {  
         try
         {
            InputStream inStream = incoming.getInputStream();
            OutputStream outStream = incoming.getOutputStream();
            
            Scanner in = new Scanner(inStream);         
            PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
            
            out.println( "Hello! Enter BYE to exit." );
            
            // echo client input
            boolean done = false;
            while (!done && in.hasNextLine())
            {  
               String line = in.nextLine();            
               out.println("Echo: " + line);            
               if (line.trim().equals("quit"))
					done = true;
				if (line.trim().equals("menu")) {
					EchoServer.menu(in, out, incoming);
				out.println("you have just quit ServerMenu.Enter quit to exit,menu to enter ServerMenu");               
              }
            }
         }catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();}
         finally
         {
            incoming.close();
         }
      }
      catch (IOException e)
      {  
         e.printStackTrace();
      }
   }

   private Socket incoming;
}


